package net.pm.core.service.pmuser.impl;

import java.awt.Toolkit;
import java.util.List;

import javax.annotation.Resource;

import net.pm.common.dao.GenericDao;
import net.pm.common.service.impl.GenericServicerImpl;
import net.pm.common.toolkit.PMRetcodeWrapper;
import net.pm.common.toolkit.PMTool;
import net.pm.common.toolkit.Pager;
import net.pm.common.toolkit.SHAEncryptUtils;
import net.pm.common.toolkit.date.DateUtils;
import net.pm.core.dao.pmuser.PMDeptDao;
import net.pm.core.entities.db.City;
import net.pm.core.entities.db.District;
import net.pm.core.entities.db.Pmdept;
import net.pm.core.entities.db.Pmuser;
import net.pm.core.entities.db.Province;
import net.pm.core.service.pmuser.PMUserServicer;

import org.springframework.stereotype.Service;

/**
 * PM用户服务类
 */
@Service("PMUserServicer")
public class PMUserServicerImpl extends GenericServicerImpl<Pmuser, Long>
		implements PMUserServicer {
	@Resource(name = "PMUserDao")
	@Override
	public void setGenericDao(GenericDao<Pmuser, Long> genericDao) {
		super.genericDao = genericDao;
	}
	
	@Resource(name="PMDeptDao")
	private PMDeptDao deptDao;
	
	@Override
	public PMRetcodeWrapper<Pmuser> login(String loginname, String passwd) {
		PMRetcodeWrapper<Pmuser> retval = null;

		final StringBuilder jpql = new StringBuilder(
				"from Pmuser pmuser where pmuser.userName = ? and pmuser.isDelete = ? ");
		Pmuser user = super.findUniqueEntityByJpql(jpql, loginname, false);

		// user为空，表示用户名不存在
		if (PMTool.isEmpty(user)) {
			retval = new PMRetcodeWrapper<Pmuser>(
					PMUserServicer.login_failed_loginname_notexists);
			return retval;
		}

		if (!user.getPassWord().equals(
				SHAEncryptUtils.getDigestOfString(passwd))) {// 用户密码不匹配，表示密码错误
			retval = new PMRetcodeWrapper<Pmuser>(
					PMUserServicer.login_failed_passwd_error, new Pmuser(
							user.getId()));
		} else if (user.getStatus().equals(Pmuser.STATUS_DISABLE)) {// 判断用户是否被禁用
			retval = new PMRetcodeWrapper<Pmuser>(
					PMUserServicer.login_failed_account_disable, new Pmuser(
							user.getId()));
		} else {
			retval = new PMRetcodeWrapper<Pmuser>(PMUserServicer.login_success,
					user);
		}
		return retval;
	}

	@Override
	public void modifyPasswd(String newpasswd, Long userid) {
		final StringBuilder jpql = new StringBuilder(
				"update Pmuser pmuser set pmuser.passWord = ? where pmuser.id = ? ");
		super.modifyByJpql(jpql, newpasswd, userid);
	}

	@Override
	public Pager<Pmuser> findUserPagerByOwner(Integer deptId, Integer pagesize,
			Integer pageno) {
		final StringBuilder builder = new StringBuilder(
				" from Pmuser pmuser where pmuser.pmdept.id  = ? order by pmuser.id desc ");
		return super.findPagerByJpql(pagesize, pageno, builder, deptId);
	}

	@Override
	public Long enableUser(Long userId) {
		final StringBuilder jpql = new StringBuilder(
				"update Pmuser pmuser set pmuser.status = ? where pmuser.id = ? ");
		if (super.modifyByJpql(jpql, Pmuser.STATUS_ENABLE, userId) > 0) {
			return PMUserServicer.enable_user_success;
		}
		return PMUserServicer.enable_user_failed;
	}

	@Override
	public Long disableUser(Long userId) {
		final StringBuilder jpql = new StringBuilder(
				"update Pmuser pmuser set pmuser.status = ? where pmuser.id = ? ");
		if (super.modifyByJpql(jpql, Pmuser.STATUS_DISABLE, userId) > 0) {
			return PMUserServicer.disable_user_success;
		}
		return PMUserServicer.disable_user_failed;
	}

	@Override
	public void modifyUserInfo(Pmuser pmuser) {

	}

	@Override
	public Long resetPasswd(String passwd, Long userId) {
		final StringBuilder builder = new StringBuilder(
				"update Pmuser pmuser set pmuser.passWord = ? where pmuser.id = ? ");
		// 加密密码
		passwd = SHAEncryptUtils.getDigestOfString(passwd);

		if (super.modifyByJpql(builder, passwd, userId) > 0) {
			return PMUserServicer.reset_passwd_success;
		}
		return PMUserServicer.reset_passwd_failed;
	}

	@Override
	public Long createUser(Pmuser user) {
		// setter 一些默认属性
		user.setIsAdmin(false); // 非管理员
		user.setIsDelete(false); // 非删除
		user.setStatus(Pmuser.STATUS_ENABLE); // 默认激活
		user.setAddTime(DateUtils.obtainCurrentDate(DateUtils.FORMAT_LONG)); // 设置添加时间
		user.setPassWord(SHAEncryptUtils.getDigestOfString(user.getPassWord())); // 密码加密

		if (PMTool.isNotEmpty(super.create(user))) {// 返回的id非等于空，创建用户成功
			return PMUserServicer.create_user_success;
		}
		return PMUserServicer.create_user_failed;
	}

	@Override
	public List<Pmuser> findUsersByDeptId(Integer deptId) {
		final StringBuilder builder = new StringBuilder(
				" from Pmuser user where user.pmdept.id = ? ");
		return super.findEntitiesByJpql(builder, deptId);
	}

	@Override
	public PMRetcodeWrapper<Pmuser> register(String loginname, String passwd) {
		List<Pmdept> pmdeptList = deptDao.findEntitiesByJpql(new StringBuilder(" from Pmdept pmdept "));
		Pmdept pmdept;
		if(pmdeptList.isEmpty()){
			pmdept = new Pmdept();
			pmdept.setDeptName("ADMIN");
			pmdept.setDeptCode("ADMIN");
			deptDao.create(pmdept);
		}else{
			pmdept = pmdeptList.get(0);
		}
		
		PMRetcodeWrapper<Pmuser> retval = null;
		Pmuser user = new Pmuser();
		user.setAddress("XX");
		user.setAddTime(DateUtils.obtainCurrentDate());
		user.setEmail("szy.foggy@gmail.com");
		user.setIdentity("123456789");
		user.setIsAdmin(true);
		user.setNickName("超级管理员");
		user.setUserName(loginname);
		user.setPassWord(passwd);
		user.setPhone("123");
		user.setSex(Pmuser.SEX_FEMALE);
		user.setStatus(Pmuser.STATUS_ENABLE);
		user.setIsDelete(false);
		user.setPmdept(pmdept);
		user.setProvince(new Province(1));
		user.setCity(new City(1));
		user.setDistrict(new District(1));
		
		super.create(user);
		
		retval = new PMRetcodeWrapper<Pmuser>(PMUserServicer.login_success,
				user);
		return retval;
	}

}
